package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.RegionQuery;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("region")
//Localhost：9999/region/list
public class RegionController{
    @Autowired
    private IRegionService regionService;
    @Autowired
    private IDestinationService destinationService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") RegionQuery qo) {
        //PageInfo  : 分页数据封装对象, Mybatis插件, 操作的是Mysql数据库
        //Page : 分页数据封装对象, spring-data 的组件, 操作是mongodb
        Page<Region> page=regionService.query(qo);
        model.addAttribute("page", page);

        return "region/list";
    }

    //异步加载数据
    @RequestMapping("/listDests")
    @ResponseBody
    public Object listDests() {
        return destinationService.list();
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Region region) {

        regionService.saveOrUpdate(region);

        return JsonResult.success();
    }
    //关联目的地操作
    @RequestMapping("/getDestByRegionId")
    @ResponseBody
    public Object getDestByRegionId(String rid){
        List<Destination> list = destinationService.queryDestByRegionId(rid);

        return list;
    }
    //修改热门
    @RequestMapping("/changeHotValue")
    @ResponseBody
    public Object changeHotValue(int hot, String id){

        regionService.changeHotValue(id, hot);


        return JsonResult.success();
    }
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete( String id){

        regionService.delete(id);


        return JsonResult.success();
    }


}
